﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using System.Data;
using System.ComponentModel;

namespace CrmControls
{
    public class CrmWordButton : System.Web.UI.WebControls.ImageButton
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DataTable DataSource
        {
            get
            {
                DataTable s = (DataTable)ViewState["DataSource"];
                return s;
            }
            set
            {
                ViewState["DataSource"] = value;
            }
        }

        protected override void OnClick(ImageClickEventArgs e)
        {
            System.Web.UI.WebControls.Table tablo = new System.Web.UI.WebControls.Table();
            tablo.GridLines = System.Web.UI.WebControls.GridLines.Both;
            GridView GridView1 = new GridView();
            GridView1.DataSource = this.DataSource;
            GridView1.DataBind();
            tablo.Rows.Add(GridView1.HeaderRow);
            foreach (GridViewRow rows in GridView1.Rows)
            {
                tablo.Rows.Add(rows);
            }
            StringWriter stringYaz = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(stringYaz);
            tablo.RenderControl(htw);
            Page.Response.Clear();
            Page.Response.AddHeader("content-disposition", "attachment;filename=Data.doc");
            Page.Response.ContentType = "application/ms-word";
            Page.Response.Write(stringYaz.ToString());
            Page.Response.End();
            base.OnClick(e);
        }
    }
}
